package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-12-10
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //读取上传的文件
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //课程分类列表
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<EduSubject>();
        wrapperOne.eq("parent_id", "0");
        //方式一
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapperOne);
        //方式二
        //List<EduSubject> list = this.list(wrapperOne);

        //2.查询所有二级分类  ne 不等于
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<EduSubject>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjects = baseMapper.selectList(wrapperTwo);

        //创建list集合用于储存所有数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //3.封装一级分类
        //遍历查询到的一级分类
        for (int i = 0; i < oneSubjects.size(); i++) {

            /*一级分类*/
            EduSubject eduSubject = oneSubjects.get(i);

            //设置前端显示的一级分类
            OneSubject oneSubject = new OneSubject();

            //简写 spring 工具类赋值 eduSubject属性名相同的值会自动赋值到oneSubject
            BeanUtils.copyProperties(eduSubject,oneSubject);

            //放入集合
            finalSubjectList.add(oneSubject);

            /*二级分类*/
            ArrayList<TwoSubject> twoFinalSubjectList = new ArrayList<>();

            for (int j = 0; j < twoSubjects.size(); j++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjects.get(j);

                //封装前端显示的二级分类 判断条件 二级父级id == 一级的id
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }

            }
            oneSubject.setChildren(twoFinalSubjectList);
        }

        return finalSubjectList;
    }
}
